<?php
/**
 * @package    Delcampe Community
 *
 * @copyright Sebastien Delcampe
 * @license Please, don't read or copy
 * @author afernando
 *
 * @version 1.0
**/
class Admin_TranslateController extends Zend_Controller_Action
{
	function init()
	{
	}
	
	public function indexAction()
 	{
    	$this->_helper->layout()->disableLayout();
 		Zend_Loader::loadFile('../application/admin/models/Translate.php', '', true);
    	Zend_Loader::loadFile('../application/default/models/Languages.php', '', true);    
	    $this->view->title = 'Translate module :: Administrator';
	    $id = $this->_getParam('id', 0);
	    $section = $this->_getParam('section', 0);
	    $translate = new Translate();
	    $languages = new model_languages();
	    $supported_lang = $languages->getSupportedLanguages();
	    $this->view->translate_entries = $translate->fetchTranslateText();
	    $this->view->supported_languages = $supported_lang;
	    
	    $langArr = $translate->getTranslateTextBySection('home');
		//print_r($langArr);
		$this->view->langArr = $langArr;
		$this->view->section = 'home';
		
		$paginator = Zend_Paginator::factory($langArr);
	    $paginator->setCurrentPageNumber($this->_getParam("page"));
	    $paginator->setItemCountPerPage(10);
	    $paginator->setPageRange(5);
	    $this->view->paginator = $paginator;
	}
	
	public function manageAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/admin/models/Translate.php');
		$request = $this->getRequest();
		$translate = new Translate();
		$section = $request->getParam("section");
		if($section == "") {
			$section = 'home';
		} else {
			$section = $section;
		}
		$langArr = $translate->getTranslateTextBySection($section);
		
		$this->view->langArr = $langArr;
		$this->view->section = $section;
		$paginator = Zend_Paginator::factory($langArr);
	    $paginator->setCurrentPageNumber($this->_getParam("page"));
	    $paginator->setItemCountPerPage(50);
	    $paginator->setPageRange(5);
	    $this->view->paginator = $paginator;
	}
	
	public function edittranslateAction()
  	{
    	$this->_helper->layout()->disableLayout();
  		Zend_Loader::loadFile('../application/admin/models/Translate.php');
    	Zend_Loader::loadFile('../application/admin/forms/Translateedit.php');
		$request = $this->getRequest();
    	$id = $request->getParam('id');
		$lang = $this->_getParam('lang');
		$section = $this->_getParam('section');
		$translate = new Translate();
		$form = new Default_Form_Translateedit();
		$this->view->translate_entries = $translate->fetchTranslateEditText($id, $lang);
		$translate->setId($id);
		$date_modified = date('Y-m-d H:i:s');
    	$this->view->lang = $lang;         
    	$this->view->id = $id;  
    	$this->view->section = $section;       
    	$this->view->title = "Edit Translations";
  	}
  
	public function updatetranslateAction() 
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/admin/models/Translate.php');
		$request = $this->getRequest();
		
		if ($this->getRequest()->isPost()) {            
			$translate_model = new Translate();
			$form_values = $request->getPost();
			$update_translate = $translate_model->saveTranslate($form_values);
		}
		
		exit;
	}
  
  public function versionAction() 
  {      
    Zend_Loader::loadFile('../application/translate/models/Translate.php');
    $id = $this->_getParam('id', 0);
    $lang = $this->_getParam('lang', 0);
    $where_clause="WHERE lv.language = '" . $lang . "' ";		
    $translate = new Translate();
 	  $this->view->version_entries = $translate->fetchTranslateVersion($where_clause);
  }
  
  public function editversionAction()
  {
    Zend_Loader::loadFile('../application/translate/models/Translate.php');
    Zend_Loader::loadFile('../application/translate/forms/Translateedit.php');
    $id = $this->_getParam('id', 0);
	  $lang = $this->_getParam('lang', '');
	  $version = $this->_getParam('version', '');
	  $translate = new Translate();
	  $form = new Default_Form_Translateedit();
	  $this->view->translate_entries = $translate->fetchVersionEditText($id, $version);
	  $translate->setId($id);
	  $request = $this->getRequest();      
    $id = $request->getParam('id');
    $lang = $request->getParam('lang');
    $id_author = Zend_Auth::getInstance()->getStorage()->read()->id_member;
    $date_modified = date('Y-m-d H:i:s');
    $this->view->lang = $lang;
    foreach ($this->view->translate_entries as $translate_entry)
    {
      $contents = $translate_entry['contents'];
      $base_version = $translate_entry['base_version'];
      $version = $translate_entry['version'];
      if($version == null)
      {
        $version = 1;
      }
      $this->view->key = $contents;
      $this->view->base_version = $base_version;
      $this->view->version = $version;
      $form->getElement('contents')->setValue($contents);  
    }     
    if ($this->getRequest()->isPost()) {            
      if ($form->isValid($request->getPost())) {
      	$model = new Translate($form->getValues());
        $model->setid($id);
        $model->setid_author($author_id);
      	$model->save();
      }
    }
    $this->view->form = $form;            
  }
  
  public function updateversionAction() 
  {
    Zend_Loader::loadFile('../application/translate/models/Translate.php');
    Zend_Loader::loadFile('../application/translate/forms/Translateedit.php');
    $registry = Zend_Registry::getInstance();
    $this->db = $registry->get('db');   
    $form = new Default_Form_Translateedit();
    $request = $this->getRequest();   
    $id = $this->_getParam('id', 0);
    $version = $this->_getParam('version', 0);
    $id_author = Zend_Auth::getInstance()->getStorage()->read()->id_member;
    $bv = $this->_getParam('bv', 0);      
    if ($this->getRequest()->isPost()) {            
      if ($form->isValid($request->getPost())) {
      	$model = new Translate();
      	$form_values = $form->getValues();
      	$model->updateVersion($form_values, $id, $version);
        //return $this->_helper->redirector('index');         
      }
    }
  }
}
